import lib_car from "../../../api/lib_car"
import _activity from "../../../cache/_activity"
import _series from "../../../cache/_series"
import _layer from "../../../common/_layer"
import _object from "../../../utils/_object"

Page({
  data: {
    curr_series: '',
    series: '',
    series_id: 0,
    series_list: [],

    activity_id: '',
    tids:0,
    clickid: ''
  },
  onLoad: function (options) {

  },
  onShow() {
    if (_activity.get() && _object._length(_activity.get()) > 0) {
      this.setData({
        activity_id: _activity.get().activity_id,
        tids: _activity.get().tids,
        clickid: _activity.get().clickid,
      })
    }

    let cach_series = _series.get()
    if (cach_series && _object._length(cach_series) > 0) {
      this.setData({
        series: cach_series.series_name,
        curr_series: cach_series.series_name,
        series_id: cach_series.series_id
      })
    }

    this.init_car(this.data.activity_id)
  },

  //请求车型
  init_car(activity_id) {
    lib_car.seriessi(activity_id).then(res => {
      if (res.code != 0) {
        _layer.message(res.msg);
        return;
      }
      this.setData({
        series_list: res.data,
      })
    })
  },

  //切换车型
  change_series(e) {
    const { item, index } = e.currentTarget.dataset;
    this.setData({ curr_series: item.name });
    _series.set({ 'series_name': item.name, "series_id": item.id })
    tt.navigateBack({
      delta: 1,
    })
  },

  //分享
  async onShareAppMessage(option) {

    const { activity_id, tids, clickid } = this.data

    return {
      title: '查询底价',
      path: `/pages/single/list/list?activity_id=${activity_id}&tids=${tids}&clickid=${clickid}`, // ?后面的参数会在转发页面打开时传入onLoad方法
      imageUrl: '/assets/image/banner.png', // 支持本地或远程图片，默认是小程序 icon
      success(res) {
        console.log('分享成功', res);
      },
      fail() {
        console.log("转发发布器调起失败");
      },
    };
  },

})